package com.lagou.server;

import com.lagou.client.ClientScanner;
import com.lagou.model.Questions;
import com.lagou.model.Score;
import com.lagou.model.User;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 编程实现数据的存取
 */
public class ServerDao {

    /**
     * 定义Map集合保存学生信息及成绩
     */
    //创建List保存学生信息
    static List<User> userList = new ArrayList<>();
    //创建List保存考题信息
    static List<Questions> questionsList = new ArrayList<>();
    //创建Map保存学生成绩
    static HashMap<String, Integer> userScoresMap = new HashMap<>();

    //创建File类型对象与文件关联
    File userFileSource = new File("/Users/wanggege/Desktop/Database/学员信息.txt");
    File examQuestionsFileSource = new File("/Users/wanggege/Desktop/Database/考题信息.txt");
    File scoreFileSource = new File("/Users/wanggege/Desktop/Database/成绩信息.txt");

    private ServerInitClose sic;

    public ServerDao(ServerInitClose sic) {
        this.sic = sic;
    }


    /**
     * 编程实现管理员和用户的账号和密码的校验并将结果返回出去
     *
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user) {
        if ("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    public boolean serverUserCheck(User user) throws IOException, ClassNotFoundException {
        getUserInfo();
        if (userList.contains(user)) {
            return true;
        }
        return false;
    }


    /**
     * 自定义成员方法实现学生信息的保存和读取
     */
    public void saveUserInfo() throws IOException {
        //创建对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(userFileSource));
        //写入数据
        oos.writeObject(userList);
        oos.flush();
    }

    public void getUserInfo() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(userFileSource));
        userList = (ArrayList<User>) ois.readObject();
    }

    public void saveQuestionsInfo() throws IOException {
        //创建对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(examQuestionsFileSource));
        //写入数据
        oos.writeObject(questionsList);
        oos.flush();
    }

    public void getQuestionsInfo() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(examQuestionsFileSource));
        questionsList = (ArrayList<Questions>) ois.readObject();
    }

    public void getScoresInfo() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(scoreFileSource));
        userScoresMap = (HashMap<String, Integer>) ois.readObject();
    }

    public void saveScoreInfo() throws IOException {
        //创建对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(scoreFileSource));
        //写入数据
        oos.writeObject(userScoresMap);
        oos.flush();
    }


    /**
     * 自定义成员方法实现学生信息的增删改查
     */

    public void addUserInfo() throws IOException, ClassNotFoundException {
        User user = (User) sic.getOis().readObject();
        userList.add(user);
        System.out.println("增加成功");

    }

    public void deleteUserInfo() throws IOException, ClassNotFoundException {
        int index = sic.getOis().read();
        userList.remove(index);
        System.out.println("删除成功");
    }

    public void updateUserInfo() throws IOException, ClassNotFoundException {
        int index = sic.getOis().read();//获取当前用户在集合的位置
        sic.getOos().writeUTF("请输入新的用户名和密码");
        sic.getOos().flush();
        User user1 = (User) sic.getOis().readObject();
        //在集合中修改信息
        userList.set(index, user1);
        System.out.println("修改成功");
        saveUserInfo();

    }

    public void queryUserInfo() throws IOException {
        int userCnt = userList.size();
        sic.getOos().write(userCnt);
        sic.getOos().flush();
        for (User user : userList) {
            sic.getOos().writeObject(user);
            sic.getOos().flush();
        }
    }

    /**
     * 自定义成员方法实现考题信息的增删改查及导入
     */

    public void addExamQuestions() throws IOException, ClassNotFoundException {
        Questions question = (Questions) sic.getOis().readObject();
        questionsList.add(question);
        System.out.println("增加成功");
    }

    public void deleteExamQuestions() throws IOException {
        int index = sic.getOis().read();
        questionsList.remove(index);
    }

    public void updateExamQuestions() throws IOException, ClassNotFoundException {
        int index = sic.getOis().read();
        Questions question = (Questions) sic.getOis().readObject();
        questionsList.set(index, question);

    }

    public void queryExamQuestions() throws IOException {
        int questionsCnt = questionsList.size();
        sic.getOos().write(questionsCnt);
        sic.getOos().flush();
        for (Questions q : questionsList) {
            sic.getOos().writeObject(q);
            sic.getOos().flush();
        }
    }

    public void importExamQuestions() throws IOException, ClassNotFoundException {
        ArrayList<Questions> questionsArrayList = (ArrayList<Questions>) sic.getOis().readObject();
        for (Questions q : questionsArrayList) {
            questionsList.add(q);
        }

    }


    /**
     * 自定义成员方法实现考试模块
     */

    public void setPassword(User user) throws IOException, ClassNotFoundException {
        int index = userList.indexOf(user);
        User user1 = (User) sic.getOis().readObject();
        //在集合中修改信息
        userList.set(index, user1);
        System.out.println("修改成功");
        saveUserInfo();


    }

    public void startExam(User user) throws IOException {
        int questionsCnt = questionsList.size();
        sic.getOos().write(questionsCnt);
        sic.getOos().flush();
        int score = 0;
        for (Questions questions : questionsList) {
            sic.getOos().writeObject(questions);
            sic.getOos().flush();
            String answer = sic.getOis().readUTF();
            if (answer.equals(questions.getAnswer())) {
                score += Integer.parseInt(questions.getScore());
            }
        }
        userScoresMap.put(user.getUserName(), score);
        saveScoreInfo();
    }

    public void queryScoresInfo(User user) throws IOException, ClassNotFoundException {
        getScoresInfo();
        Integer score = userScoresMap.get(user.getUserName());
        sic.getOos().write(score);
        sic.getOos().flush();
    }

    public void exportScoreInfo(User user) throws IOException, ClassNotFoundException {
        getScoresInfo();
        Integer score = userScoresMap.get(user.getUserName());
        Score score1 = new Score(user.getUserName(), score);
        sic.getOos().writeObject(score1);
    }
}
